package com.wenlin.userservice.util;

import com.wenlin.userservice.controller.UserController;
import com.wenlin.userservice.service.TestService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.StringRedisTemplate;


/***
 *  Created with IntelliJ IDEA.
 *  User:  wenlin
 *  Date:  2017/12/1
 *  Time:  13:19
 *  Description:
 **/

public class Switch {

    static Logger logger = LoggerFactory.getLogger(Switch.class);

    private volatile static Switch instance;

    private Integer flag;
    //通过application获取redis实例
    private StringRedisTemplate redisTemplate = SpringContextUtils.getBean(StringRedisTemplate.class);
    //通过application获取service实例
    private TestService testService = SpringContextUtils.getBean(TestService.class);
    //构造器私有化
    private Switch() {
    }
    //静态方法供外部调用
    public static Switch getInstance() {
        if (instance == null) {
            synchronized (Switch.class) {
                if (instance == null) {
                    instance = new Switch();
                }
            }
        }
        return instance;
    }
    /***
     *  User:  wenlin
     *  Date:  2017/12/1
     *  Time:  14:01
     *  Description:获取开关状态
     **/
    public Integer getFlag() {
        if (null == flag) {
            //String flags = redisTemplate.opsForValue().get("test");
            flag = testService.get().getData().getFlag();
            logger.info("数据库查找开关状态值" + "开关状态为:" + flag);
            return flag;
        }
        logger.info("单例中保存的开关状态值，状态为:" + flag);
        return flag;
    }
    /***
     *  User:  wenlin
     *  Date:  2017/12/1
     *  Time:  14:06
     *  Description:重新获取flag的值
     **/
    public void setFlag() {
        flag = testService.get().getData().getFlag();
    }
}
